package com.fuego.creditsystemcommodity.controller;

import jakarta.annotation.Resource;
import org.example.dto.Result;
import org.example.entity.Good;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/click")
public class ClickController {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @PostMapping("/hospital")
    public Result clickHospital(@RequestParam("userId") Long userId) {
        // 验证参数
        if (userId == null) {
            return Result.fail("用户ID不能为空");
        }

        try {
            // 插入点击记录
            String insertSql = "INSERT INTO tb_hospital_click (user_id) VALUES (:userId)";
            // 使用 NamedParameterJdbcTemplate
            NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
            int rowsAffected = namedParameterJdbcTemplate.update(insertSql,
                    new MapSqlParameterSource()
                            .addValue("userId", userId)
            );

            if (rowsAffected > 0) {
                return Result.ok();
            } else {
                return Result.fail("插入点击记录失败");
            }
        } catch (Exception e) {
            return Result.fail("数据库操作失败: " + e.getMessage());
        }
    }
    @PostMapping("/library")
    public Result clickLibrary(@RequestParam("userId") Long userId) {
        // 验证参数
        if (userId == null) {
            return Result.fail("用户ID不能为空");
        }

        try {
            // 插入点击记录
            String insertSql = "INSERT INTO tb_library_click (user_id) VALUES (:userId)";
            // 使用 NamedParameterJdbcTemplate
            NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
            int rowsAffected = namedParameterJdbcTemplate.update(insertSql,
                    new MapSqlParameterSource()
                            .addValue("userId", userId)
            );

            if (rowsAffected > 0) {
                return Result.ok();
            } else {
                return Result.fail("插入点击记录失败");
            }
        } catch (Exception e) {
            return Result.fail("数据库操作失败: " + e.getMessage());
        }
    }
    @PostMapping("/parking")
    public Result clickParking(@RequestParam("userId") Long userId) {
        // 验证参数
        if (userId == null) {
            return Result.fail("用户ID不能为空");
        }

        try {
            // 插入点击记录
            String insertSql = "INSERT INTO tb_parking_click (user_id) VALUES (:userId)";
            // 使用 NamedParameterJdbcTemplate
            NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
            int rowsAffected = namedParameterJdbcTemplate.update(insertSql,
                    new MapSqlParameterSource()
                            .addValue("userId", userId)
            );

            if (rowsAffected > 0) {
                return Result.ok();
            } else {
                return Result.fail("插入点击记录失败");
            }
        } catch (Exception e) {
            return Result.fail("数据库操作失败: " + e.getMessage());
        }
    }
    @PostMapping("/transportation")
    public Result clickTransportation(@RequestParam("userId") Long userId) {
        // 验证参数
        if (userId == null) {
            return Result.fail("用户ID不能为空");
        }

        try {
            // 插入点击记录
            String insertSql = "INSERT INTO tb_transportation_click (user_id) VALUES (:userId)";
            // 使用 NamedParameterJdbcTemplate
            NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
            int rowsAffected = namedParameterJdbcTemplate.update(insertSql,
                    new MapSqlParameterSource()
                            .addValue("userId", userId)
            );

            if (rowsAffected > 0) {
                return Result.ok();
            } else {
                return Result.fail("插入点击记录失败");
            }
        } catch (Exception e) {
            return Result.fail("数据库操作失败: " + e.getMessage());
        }
    }
}
